﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UbbTimetable2
{
    /// <summary>
    /// A class containing usefull data filling and checking methods.
    /// </summary>
    class Utilities
    {
        /// <summary>
        /// Used to list all the classes.
        /// </summary>
        public const int CLASSES_ALL = 0;
        /// <summary>
        /// Used to display only unscheduled classes.
        /// </summary>
        public const int CLASSES_UNSCHEDULED = 1;
        /// <summary>
        /// Used to fill the data grid with only class names.
        /// </summary>
        public const int CLASSNAMES = 2;

        /// <summary>
        /// Fills a data grid view with classes from a classes data access object.
        /// </summary>
        /// <param name="classesDao">The classes data access object.</param>
        /// <param name="dgv">The data grid view.</param>
        public static void FillDgv(ClassesDao classesDao, DataGridView dgv)
        {
            FillDgv(classesDao, dgv, CLASSES_ALL);
        }
        
        /// <summary>
        /// Fills a data grid view with classes from a classes data access object.
        /// </summary>
        /// <param name="classesDao">The classes data access object.</param>
        /// <param name="dgv">The data grid view.</param>
        /// <param name="rule">The rule that will be used to fill it. <see cref="CLASSES_ALL"/> <see cref="CLASSES_UNSCHEDULED"/> <see cref="CLASSNAMES"/></param>
        public static void FillDgv(ClassesDao classesDao, DataGridView dgv, int rule)
        {
            dgv.Rows.Clear();
            if (rule == CLASSNAMES)
            {
                List<ClassNames> list = classesDao.GetClassNames();
                foreach (ClassNames item in list)
                {
                    dgv.Rows.Add(item.ClassId, item.ClassName);
                }
            }
            else
            {
                List<Classes> list;
                switch (rule)
                {
                    case CLASSES_UNSCHEDULED:
                        list = classesDao.GetClassesList(ClassesDao.UNSCHEDULED);
                        break;
                    default:
                        list = classesDao.GetClassesList();
                        break;
                }

                foreach (Classes item in list)
                {
                    dgv.Rows.Add(item.Id, item.TypeName, item.ClassName, item.ProfFullName);
                }
            }
        }

        /// <summary>
        /// Fills a data grid view with teachings from a teaching data access object.
        /// </summary>
        /// <param name="teachingDao">The teaching data access object.</param>
        /// <param name="dgv">The data grid view.</param>
        public static void FillDgv(TeachingDao teachingDao, DataGridView dgv)
        {
            dgv.Rows.Clear();
            List<Teaching> list = teachingDao.GetTeachingList(0, "any", 0, 0, 0, 0);
            foreach (Teaching item in list)
            {
                dgv.Rows.Add(item.DayData.DayName,
                    item.StartTime.ToString() + "-" + item.EndTime.ToString(),
                    item.ClassroomData.CrName, item.SubgroupData.SgName,
                    item.ClassData.TypeName, item.ClassData.ClassName,
                    item.ClassData.ProfFullName, item.DayData.DayId, item.ClassData.Id,
                    item.StartTime, item.EndTime);
            }
        }

        /// <summary>
        /// Fills a data grid view with professors from a professor data access object.
        /// </summary>
        /// <param name="classesDao">The professor data access object.</param>
        /// <param name="dgv">The data grid view.</param>
        public static void FillDgv(ProfDao profDao, DataGridView dgv)
        {
            dgv.Rows.Clear();
            List<Prof> list = profDao.GetProfList();
            foreach (Prof item in list)
            {
                dgv.Rows.Add(item.Cnp,item.Lname,item.Fname,item.Tel,item.Email,item.Rank,item.Hours.ToString());
            }
        }

        /// <summary>
        /// Fills a data grid view with subgroups from a subgroup data access object.
        /// </summary>
        /// <param name="classesDao">The subgroup data access object.</param>
        /// <param name="dgv">The data grid view.</param>
        public static void FillDgv(SubgroupDao sgDao, DataGridView dgv)
        {
            dgv.Rows.Clear();
            List<Subgroup> list = sgDao.GetSubgroupList("any");
            foreach (Subgroup item in list)
            {
                dgv.Rows.Add(item.SgId, item.GroupName, item.SgName);
            }
        }

        /// <summary>
        /// Tells if a number can be considered a time value.
        /// </summary>
        /// <param name="number">The number that will be checked.</param>
        /// <returns>True if it's a time value and false otherwise.</returns>
        public static bool IsTime(int number)
        {
            return number >= 0 && number < 2400;
        }
    }
}
